package com.example.blog.service.impl;

import com.example.blog.dao.ArticleDao;
import com.example.blog.dao.CommentDao;
import com.example.blog.dao.UserDao;
import com.example.blog.dto.ArticleDTO;
import com.example.blog.dto.CommentDTO;
import com.example.blog.pojo.PageBean;
import com.example.blog.pojo.User;
import com.example.blog.service.AdminService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private CommentDao commentDao;


    //获取用户列表
    @Override
    public PageBean<User> getUserList(Integer pageNum, Integer pageSize, Integer role) {
        //1.创建PageBean对象
        PageBean<User> pb = new PageBean<>();

        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);

        //3.调用mapper
//        Map<String,Object> map = ThreadLocalUtil.get();
//        Integer userId = (Integer) map.get("id");
        List<User> as = userDao.findAllUser(role);
        //Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据
        Page<User> p = (Page<User>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    //获取文章列表
    @Override
    public PageBean<ArticleDTO> getArticleList(Integer pageNum, Integer pageSize, Integer categoryId, Integer status) {
        //1.创建PageBean对象
        PageBean<ArticleDTO> pb = new PageBean<>();

        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);

        //3.调用mapper
//        Map<String,Object> map = ThreadLocalUtil.get();
//        Integer userId = (Integer) map.get("id");
        List<ArticleDTO> as = articleDao.findArticle(categoryId,status);
        //Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据
        Page<ArticleDTO> p = (Page<ArticleDTO>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }


    //获取评论列表
    @Override
    public PageBean<CommentDTO> getCommentList(Integer pageNum, Integer pageSize, Integer articleid, Integer userid) {
        //1.创建PageBean对象
        PageBean<CommentDTO> pb = new PageBean<>();

        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);

        //3.调用mapper
//        Map<String,Object> map = ThreadLocalUtil.get();
//        Integer userId = (Integer) map.get("id");
        List<CommentDTO> as = commentDao.findByUidandAid(userid,articleid);
        //Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据
        Page<CommentDTO> p = (Page<CommentDTO>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    //更新审核状态
    @Override
    public boolean updateArticle(Integer id, String message,Integer status) {

        Integer n = articleDao.updateStatus(id,status,message, LocalDateTime.now());
        if(n>0)return true;
        else return false;

    }

    //删除评论
    @Override
    public boolean deleteComment(Integer id) {

        Integer n = commentDao.deleteComment(id);
        if(n>0)return true;
        else return false;

    }

    //删除用户
    @Override
    public boolean deleteUser(Integer userid) {

        Integer n = userDao.deleteById(userid);
        if(n>0)return true;
        else return false;

    }

    //文章内容
    @Override
    public String getArticleContent(Integer id) {

        String content = articleDao.findContentById(id);
        return content;

    }
}
